<?php

namespace app\admin\model;

use app\admin\common\Common;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\facade\Request;
use think\Model;

class UserModel extends Model
{
    protected $pk = 'id';

    public static function updateuser($data){
        $update = [
            'id' => $data['id'],
            'nickname' => $data['nickname'],
            'usertx' => $data['usertx'],
            'qq' => $data['qq'],
            'useremail' => $data['useremail'],
            'signature' => $data['signature'],
            'title' => $data['title'],
            'viptime' => strtotime($data['viptime']),
            'money' => $data['money'],
            'exp' => $data['exp'],
            'banned' => $data['banned'],
            'banned_reason' => $data['banned_reason'],
        ];
        try {
            $user = Db::name('user')->where('id', $data['id'])->update($update);
        } catch (PDOException $e) {
             return Common::result(400,$e->getMessage());
        } catch (Exception $e) {
             return Common::result(400,$e->getMessage());
        }
        if ($user>0){
            return Common::result(200,'修改成功');
        }else{
            return Common::result(400,'你未做任何修改！');
        }
    }

    public static function adduser($data){
        try {
            $app = Db::name('app')->where('appid', $data['appid'])->find();
            $adminemail = Db::name('user')->where('useremail', $data['useremail'])->find();
        } catch (DataNotFoundException $e) {
             return Common::result(400,$e->getMessage());
        } catch (ModelNotFoundException $e) {
             return Common::result(400,$e->getMessage());
        } catch (DbException $e) {
             return Common::result(400,$e->getMessage());
        }
        if ($adminemail['useremail'] == null || $adminemail['useremail'] == ""){
            $adddata = [
                'username' => $data['username'],
                'password' => md5($data['password']),
                'useremail' => $data['useremail'],
                'appid' => $data['appid'],
                'usertx' => Request::scheme() . "://" . Request::host() . "/" . "usertx.png",
                'viptime' => time()+($app['zcvip']*24*3600),
                'money' => $app['zcmoney'],
                'exp' => $app['zcexp'],
                'creattime' => time(),
            ];
            if ($app == null){
                return Common::result(400,'app不存在');
            }else{
                $user = Db::name('user')->data($adddata)->insert();
                if ($user>0){
                    return Common::result(200,'添加成功');
                }else{
                    return Common::result(400,'添加失败');
                }
            }
        }else{
            return Common::result(400,'邮箱已存在');
        }

    }

    public static function deluser($data){
        try {
            $user = Db::name('user')->delete($data['id']);
        } catch (PDOException $e) {
             return Common::result(400,$e->getMessage());
        } catch (Exception $e) {
             return Common::result(400,$e->getMessage());
        }
        if ($user>0){
            return Common::result(200,"删除成功");
        }else{
            return Common::result(400,'删除失败');
        }
    }

    public static function searchuser($data){
        try {
            $sql = Db::name('user')
                ->alias('u')
                ->where('username', $data['searchusername'])
                ->join('app a', 'a.appid = u.appid')
                ->field('u.*,FROM_UNIXTIME(u.creattime,"%Y-%m-%d %T") as time,a.appname')
                ->select();
        } catch (DataNotFoundException $e) {
             return Common::result(400,$e->getMessage());
        } catch (ModelNotFoundException $e) {
             return Common::result(400,$e->getMessage());
        } catch (DbException $e) {
             return Common::result(400,$e->getMessage());
        }
        if (sizeof($sql) == 0){
            return Common::result(400,"暂无查询到数据");
        }else{
            return Common::result(200,"查询成功",$sql);
        }
    }

}